﻿using System;

//自定义
using SpeechLib;

namespace DreamCube.Foundation.Speech
{
    public class VoicePlayer
    {
        #region "私有变量"

        private SpVoice voice = null;

        #endregion

        public VoicePlayer()
        {
            voice = new SpVoice();
        }

        /// <summary>
        /// 朗读信息
        /// </summary>
        /// <param name="msg">信息文本</param>
        /// <param name="times">次数；默认为1次</param>
        /// <param name="timeIntervalSeconds">时间间隔；非常注意：此时间间隔是不包括朗读时间的，朗读完毕之后，等待下一次重复朗读的时间；默认为1秒</param>
        public void SpeakMsg(String msg, Int32 times = 1, Int32 timeIntervalSeconds = 1)
        {
            Speak(msg);
        }

        /// <summary>
        /// 把文本生成声音文件
        /// </summary>
        /// <param name="fileName"></param>
        public void SpeakMsgToSaveAudioFile(String msg,String fileName)
        {
            SpFileStream stream = new SpFileStream();
            stream.Open(fileName, SpeechStreamFileMode.SSFMCreateForWrite, false);
            voice.AudioOutputStream = stream;
            voice.Speak(msg, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            voice.WaitUntilDone(System.Threading.Timeout.Infinite);
            stream.Close();
        }

        /// <summary>
        /// 朗读文本
        /// </summary>
        /// <param name="msg"></param>
        private void Speak(String msg)
        {
            voice.Speak(msg, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }
    }
}
